package com.exchange.employee.service.impl;

import com.exchange.employee.config.EsPage;
import com.exchange.employee.config.EmployeeRepository;
import com.exchange.employee.dto.EsQueryDto;
import com.exchange.employee.entity.Employee;
import com.exchange.employee.service.EmployeeService;
import com.exchange.utils.result.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.domain.Page;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

@Component
@RefreshScope
@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeRepository employeeRepository;

    @Autowired
    private ElasticsearchRestTemplate restTemplate;

    @Value("${params.es.aboutMath}")
    private String aboutMath;
    @Value("${params.es.nameMath}")
    private String nameMath;

    private EsPage esPage = new EsPage(1, 5);


    @Override
    public CommonResult getEmployeeList(EsQueryDto queryDto) {
        esPage.setPageNumber(queryDto.getCurrent());
        esPage.setPageSize(queryDto.getPageSize());

        Page<Employee> restInfo = employeeRepository.findByAboutContainsOrLast_nameContains(
                queryDto.getQueryKey(), aboutMath, queryDto.getQueryKey(), nameMath, esPage);

        return CommonResult.successWithData("员工信息查询成功", restInfo);

        /*

        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();


        QueryBuilder searchQuery = new QueryStringQueryBuilder(queryDto.getQueryKey());
        searchQuery.queryName("about");
        Page<Employee> pageInfo = employeeRepository.search(searchQuery, ePage);
        // 根据ID查询
        Optional<Employee> optional = employeeRepository.findById(1L);
        Employee employee = optional.get();
        String [] keys = queryDto.getQueryKey().split(" ");
        int total = employeeRepository.countByAboutContainsAndAboutContains(keys[0], keys[1]);
        Page<Employee> listByAbout = employeeRepository.findByAboutContains(queryDto.getQueryKey(), minimunMath, esPage);

        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
        //结果过滤
        queryBuilder.withSourceFilter(new FetchSourceFilter(new String[]{"first_name", "last_name","age", "about", "interests"}, null));

        //分页
        queryBuilder.withPageable(PageRequest.of(page, size));
        //过滤
        queryBuilder.withQuery(QueryBuilders.matchQuery("about", searchStr).minimumShouldMatch(minimunMath));

        //查询
        AggregatedPage<Employee> result = template.queryForPage(queryBuilder.build(), Employee.class);

        List<Employee> employeeList = template.queryForList(queryBuilder.build(), Employee.class);

        //解析结果
        //分页结果解析
        long total = result.getTotalElements();
        Long totalPages = total % size == 0 ? total / size : total / size + 1;
        //List<Employee> employeeList = result.getContent();
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("total", total);
        resultMap.put("totalPages", totalPages);
        resultMap.put("dataList", employeeList);
        return CommonResult.successWithData("查询员工信息成功", resultMap);*/
    }
}
